package com.atguigu.java8.stream;

import com.atguigu.java8.lambda.Employee;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class TestStreamAPI4 {

    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99, Employee.Status.FREE),
            new Employee("李四", 38, 5555.99, Employee.Status.BUSY),
            new Employee("王五", 50, 6666.66, Employee.Status.VOCATION),
            new Employee("赵六", 16, 3333.33, Employee.Status.FREE),
            new Employee("田七", 8, 7777.77, Employee.Status.BUSY)
    );

    @Test
    public void test1() {
        Integer[] nums = new Integer[]{1, 2, 3, 4, 5};
        Arrays.stream(nums).map(x -> x * x).forEach(System.out::println);
    }

    @Test
    public void test2() {
        Optional<Integer> sum = employees.stream().map(e -> 1).reduce(Integer::sum);
        System.out.println(sum.get());
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    private static class Trader {

        private String name;

        private String city;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    private static class Transaction {

        private Trader trader;

        private int year;

        private int value;

    }

    private List<Transaction> transactions = null;

    @Before
    public void init() {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );
    }

    @Test
    public void test3() {
        transactions.stream().filter(t -> t.getYear() == 2011)
                .sorted(Comparator.comparingDouble(Transaction::getValue))
                .forEach(System.out::println);
    }

    @Test
    public void test4() {
        transactions.stream().map(t -> t.getTrader().getCity())
                .distinct().forEach(System.out::println);
    }

    @Test
    public void test5() {
        transactions.stream().filter(t -> t.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getTrader)
                .distinct()
                .sorted(Comparator.comparing(Trader::getName))
                .forEach(System.out::println);
    }

    @Test
    public void test6() {
        transactions.stream().map(t -> t.getTrader().getName())
                .distinct()
                .sorted().forEach(System.out::println);

        System.out.println("----------------------------------");
        String allName = transactions.stream().map(t -> t.getTrader().getName())
                .reduce("", String::concat);
        System.out.println(allName);

        System.out.println("----------------------------------");
        allName = transactions.stream().map(t -> t.getTrader().getName())
                .collect(Collectors.joining(","));
        System.out.println(allName);
    }

    @Test
    public void test7() {
        boolean match = transactions.stream().anyMatch(e -> e.getTrader().getCity().equals("Milan"));
        System.out.println(match);
    }

    @Test
    public void test8() {
        Optional<Integer> sum = transactions.stream().filter(e -> e.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getValue).reduce(Integer::sum);
        System.out.println(sum.get());

        System.out.println("----------------------------------------------------------------------");
        int sum1 = transactions.stream().filter(e -> e.getTrader().getCity().equals("Cambridge"))
                .mapToInt(Transaction::getValue).sum();
        System.out.println(sum1);
    }

    @Test
    public void test9() {
        Optional<Integer> max = transactions.stream().map(Transaction::getValue).max(Integer::compare);
        System.out.println(max.get());
    }

    @Test
    public void test10() {
        Optional<Transaction> min = transactions.stream().min(Comparator.comparing(Transaction::getValue));
        System.out.println(min.get());
    }
}
